import 'package:build/build.dart';
import 'package:source_gen/source_gen.dart';
import 'src/generator/entity_generator.dart';
import 'src/generator/dao_generator.dart';
import 'src/generator/dao_autoscan.dart';

/// 实体增强代码生成器的Builder配置
/// 为build_runner提供PartBuilder，生成.g.dart part文件
Builder entityBuilder(BuilderOptions options) {
  // 读取options中的typeconvert类的路径,如果path为空则使用默认的路径
  final typeConvertPath = options.config['typeConvertPath'] as String? ??
      "lib/src/type_converter.dart";

  return PartBuilder(
    [EntityGenerator(typeConvertPath)],
    '.g.dart',
    header: '''
// GENERATED CODE - DO NOT MODIFY BY HAND
// Entity enhanced code generated by fall_data_gen

''',
    options: options,
  );
}

/// DAO代码生成器的Builder配置
/// 为build_runner提供PartBuilder，生成.g.dart part文件
Builder daoBuilder(BuilderOptions options) {
  // 读取options中的typeconvert类的路径,如果path为空则使用默认的路径
  final typeConvertPath = options.config['typeConvertPath'] as String? ??
      "lib/src/type_converter.dart";

  return LibraryBuilder(
    DaoGenerator(typeConvertPath),
    header: '''
// GENERATED CODE - DO NOT MODIFY BY HAND
// DAO implementation generated by fall_data_gen

''',
    options: options,
  );
}

/// DAO自动扫描代码生成器的Builder配置
/// 为build_runner提供PartBuilder，生成服务自动注册代码
Builder daoAutoScanBuilder(BuilderOptions options) {
  bool debug = false;
  if (options.config['debug'] == true) {
    debug = true;
  }
  print(debug);
  return LibraryBuilder(
    DaoAutoScan(debug: debug),
    header: '''
// GENERATED CODE - DO NOT MODIFY BY HAND
// DAO auto-scan implementation generated by fall_data_gen

''',
    options: options,
  );
}
